package com.djf.djfclickhouse.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import ru.yandex.clickhouse.ClickHouseDataSource;
import ru.yandex.clickhouse.settings.ClickHouseProperties;

import javax.sql.DataSource;

/**
 * ClickHouse数据源配置
 */
@Configuration
public class ClickHouseConfig {
    
    @Value("${clickhouse.url:jdbc:clickhouse://localhost:8123/default}")
    private String url;
    
    @Value("${clickhouse.username:default}")
    private String username;
    
    @Value("${clickhouse.password:}")
    private String password;
    
    @Bean
    public DataSource clickHouseDataSource() {
        ClickHouseProperties properties = new ClickHouseProperties();
        properties.setUser(username);
        properties.setPassword(password);
        properties.setSocketTimeout(30000);
        properties.setConnectionTimeout(10000);
        
        return new ClickHouseDataSource(url, properties);
    }
}